package la.bbs558.aop.valid;


import cn.hutool.core.util.NumberUtil;
import la.bbs558.util.Objs;

import java.util.function.Function;

/**
 * @author KLJ
 */

public enum ValidEnum {
    TEST(ValidEnum::test),
    NULL(Objs::isNull),
    NOT_NULL(Objs::isNotNull),
    NUMBER(ValidEnum::isNumber),
    IS_VALID(Objs::isValid);
    private final Function<Object, Boolean> function;
    ValidEnum(Function<Object, Boolean> function) {
        this.function = function;
    }

    public void check(Object params,String msg){
        if (!function.apply(params)) {
            throw new ValidException(msg);
        }
    }

    public Boolean check(Object params){
       return function.apply(params);
    }

    public static Boolean test(Object params){
        return "test".equals(params);
    }

    public static Boolean isNumber(Object params){
        return Objs.isValid(params) && NumberUtil.isNumber(params.toString());
    }

}
